package com.lanmao.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanmao.blog.entity.Type;
import com.lanmao.blog.service.TypeService;
import com.lanmao.blog.utils.R;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lanmao
 * @since 2022-03-13
 */
@RestController
@RequestMapping("/type")
public class TypeController {
    @Autowired
    TypeService typeService;

    /**
     * 查询所有分类
     * @return
     */
    @RequestMapping("getAllType")
    public R getAllType(){
          List<Type> type=typeService.getAllType();
          return R.ok().data("data",type);
    }

    /**
     * 保存分类
     * @param type
     * @return
     */
    @PostMapping("/saveType")
    public R saveType(@RequestBody Type type){
        if (StringUtils.isEmpty(type.getName())){
            return R.error().message("分类专栏名称不能为空！");
        }
        QueryWrapper<Type> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",type.getName());
        List<Type> blogList = typeService.list(queryWrapper);
        if (blogList.size() > 0){
            return R.error().message("添加失败，已有该分类专栏名称");
        }
        boolean save = typeService.saveType(type);
        if (save){
            return R.ok().message("添加成功！");
        }else {
            return R.error().message( "添加失败！");
        }

    }



    /**
     * 分类的分页查询
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getTypeByPage")
    @ApiOperation("分类的分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页") ,
            @ApiImplicitParam(name = "size",value = "每页的数量"),
    })
    public R getByPage(Long current, Long size){
        Page<Type> typePage= typeService.pageTypes(current, size);
        return R.ok().data("data",typePage);
    }

    /**
     * 通过名称查找分类
     * @param name
     * @return
     */
    @GetMapping("/getTypeByName")
    public R getTypeByName(String name){
        List<Type> list= typeService.getTypeByName(name);
        return R.ok().data("data",list);
    }

    /**
     * 更新分类
     * @param type
     * @return
     */
    @PutMapping("/updateType")
    public R updateType(@RequestBody Type type){
        if (StringUtils.isEmpty(type.getName())){
            return  R.error().message("分类名称不能为空");
        }
        boolean flag = typeService.updateById(type);
        if (flag) {
            return R.ok().message("更新分类成功");
        }
        return R.error().message("更新分类失败");
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping("deleteTypeById")
    public R deleteTypeById(Long id){
        boolean flag= typeService.removeById(id);
        if (flag){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    /**
     * 根据id查询分类
     * @param id
     * @return
     */
    @GetMapping("/getTypeById")
    public R getTypeById(Long id){
        Type type= typeService.getById(id);
        return R.ok().data("data",type);
    }


}

